<template>
	<view class="container">
		<view class="course_cover">
			<image @tap="preview(examData.examInfo.coverPic)" :src="examData.examInfo.coverPic" mode=""></image>
		</view>
		<view class="course_name">
			<view class="course_name_text">
				<text>{{examData.examInfo.examName}}</text>
				<image :src="iconUrl + '88.png'" mode=""></image>
			</view>
			<view class="course_name_tips">
				{{examData.examInfo.examTime}}分钟<text>|</text>
				{{examData.examInfo.totalQuestion}}道题 <text>|</text>
				总分{{examData.examInfo.totalScore}}
			</view>
		</view>
		<view class="related_courses">
			<view class="related_courses_title">关联课程</view>
			<view class="related_courses_itme">
				<image class="related_courses_itme_img" :src="examData.examInfo.courseVO.courseCover" mode=""></image>
				<view class="related_courses_itme_text">{{examData.examInfo.courseVO.courseName}}</view>
			</view>
		</view>
		<view class="exam_details" v-if="examData.examInfo.examDetails">
			<view class="exam_details_title">考试详情</view>
			<view class="">
				<!-- <rich-text :nodes="dealHtml(examData.examInfo.examDetails || '')"></rich-text> -->
			<!-- 	<view v-html="dealHtml(examData.examInfo.examDetails || '')"></view> -->
				<mp-html :content='dealHtml(examData.examInfo.examDetails)'></mp-html>
			</view>
		</view>
		<view class="examination_record" v-if="examData.examRecordList.length > 0">
			<view class="examination_record_title">考试记录</view>
			<view class="examination_record_item" v-for="(item,index) in examData.examRecordList" :key="index"
				@tap="goExamDetails(item.id)">
				<view class="examination_record_item_top">
					<view class="">交卷时间：{{item.submitTime}}</view>
					<text class="">· {{examStatusText(item.status)}}</text>
				</view>
				<view class="examination_record_item_bottom">[分数：{{item.score}}]</view>
				<view class="see_certificate" v-if="item.isCertificate == 0" @tap.stop="seeCertificate(item.id)">查看证书
				</view>
			</view>
		</view>
		<view class="bottom_btn">
			<view class="xjs-btn-primary" @tap="goPartExam(examData.examId)" v-if="
			examData.examInfo.status == 1 && 
			examData.examInfo.remainingNum > 0 && 
			(examData.examRecordList.length == 0 || ![0,5].includes(Number(examData.examRecordList[0].status)))">参加考试</view>
			<view class="xjs-btn-primary xjs-btn-disabled" v-if="examData.examInfo.status == 2">考试已下架</view>
		</view>


	</view>
</template>

<script lang="ts" setup>
	import { dealHtml } from '@/hooks/useFunction'
	import api from '@/api'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { ref, reactive } from 'vue'
	import { useUserStoreToRefs } from '@/store/useUserStore'
	const { userInfo } = useUserStoreToRefs()
	const iconUrl = ref("https://xjs-oss-prod.oss-cn-shenzhen.aliyuncs.com/xjs-static/icon-")
	onLoad(async (options) => {
		examData.examId = options.examId


	})
	onShow(async () => {
		await examData.getCourseExamInfo(examData.examId)
		await examData.getCourseExamRecordList({ examId: examData.examId })
	})

	const examData = reactive({
		examInfo: {},
		examRecordList: [],
		examId: '',
		// 考试信息
		getCourseExamInfo: async (id) => {
			const res : any = await api({
				method: 'get',
				url: `/mall/exam/info/${id}`
			})
			examData.examInfo = res.data || {}
		},
		//考试记录
		getCourseExamRecordList: async (data) => {
			const res : any = await api({
				method: 'get',
				url: `/mall/exam/exam-record/getExamRecordList`,
				data
			})
			examData.examRecordList = res.data || []
		}
	})
	const goPartExam = (examId) => {
		examData.examInfo.examDetails = ''
		uni.navigateTo({
			url: `/pages/businessSchool/partExam/partExam?examId=${examId}`
		})
	}
	const goExamDetails = (id) => {
		
		if (!userInfo.value) {
			uni.navigateTo({
				url: '/pages/login/index'
			})
			return
		}
		examData.examInfo.examDetails = ''
		uni.navigateTo({
			url: `/pages/businessSchool/examDetails/index?id=${id}`
		})
	}
	const seeCertificate = (id) => {
		examData.examInfo.examDetails = ''
		uni.navigateTo({
			url: `/pages/share/workCode/index?type=4&id=${id}`
		})
	}
	const examStatusText = (val) => {
		let text = ''
		switch (Number(val)) {
			case 0:
				text = '考试待批阅'
				break;
			case 1:
				text = '批阅已通过'
				break;
			case 2:
				text = '批阅未通过'
				break;
			case 3:
				text = '考试未通过'
				break;
			case 4:
				text = '考试中'
				break;
			case 5:
				text = '考试通过'
				break;

		}
		return text
	}
	const preview = (imgUrl) => {
		let images = [imgUrl]
		console.log(images, 'images')
		uni.previewImage({
			current: 0,
			urls: images,
		});
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #fff;
		min-height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 100rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 100rpx);
	}

	.course_cover {
		width: 100%;
		height: 422rpx;

		image {
			width: 100%;
			height: 422rpx;
		}
	}

	.course_name {
		padding: 48rpx 32rpx;
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
		border-bottom: 1rpx solid #CCCCCC;

		.course_name_text {
			display: flex;
			justify-content: space-between;
			margin-bottom: 16rpx;

			text {
				width: 596rpx;
				display: inline-block;
				font-size: 36rpx;
				color: #1A1A1A;
				font-weight: bold;
				line-height: 1.2;
			}

			image {
				margin-top: 0rpx;
				width: 48rpx;
				height: 48rpx;
			}
		}

		.course_name_tips {
			display: flex;
			align-items: center;
			margin-top: 10rpx;
			line-height: 1;
			font-size: 30rpx;
			color: #7C807F;

			text {
				margin: 0 16rpx;
				font-size: 24rpx;
				color: #D9D9D9;
			}
		}
	}

	.related_courses {
		padding: 40rpx;
		border-bottom: 1rpx solid #CCCCCC;

		.related_courses_title {
			margin-bottom: 24rpx;
			font-size: 36rpx;
			color: #1A1A1A;
			font-weight: bold;
		}

		.related_courses_itme {
			display: flex;
			justify-content: space-between;

			.related_courses_itme_img {
				width: 282rpx;
				height: 160rpx;
				border-radius: 24rpx;
			}

			.related_courses_itme_text {
				width: 370rpx;
				font-weight: bold;
				word-break: break-all;
			}
		}
	}

	.exam_details {
		padding: 48rpx 32rpx;
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;

		.exam_details_title {
			margin-bottom: 40rpx;
			font-size: 36rpx;
			color: #1A1A1A;
			font-weight: bold;
		}
	}

	.examination_record {
		padding: 48rpx 40rpx;
		padding-top: 64rpx;
		background-color: #fff;

		border-top: 1rpx solid #CCCCCC;

		.examination_record_title {
			padding-bottom: 24rpx;
			font-size: 32rpx;
			color: #1B2120;
			font-weight: bold;
			line-height: 1;
		}

		.examination_record_item {
			margin-top: 16rpx;
			padding: 32rpx 24rpx;
			box-sizing: border-box;
			background-color: #F9F6F6;
			border-radius: 16rpx;
			line-height: 1;
			position: relative;

			.examination_record_item_top {
				margin-bottom: 16rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				view {
					font-size: 28rpx;
					color: #5C5E5E;
				}

				text {
					font-size: 22rpx;
					color: #1B2120;
				}

				.via_color {
					color: #08B66E;
				}

				.not_via_color {
					color: #FF5253;
				}
			}

			.examination_record_item_bottom {
				font-size: 22rpx;
				color: #7C807F;
			}

			.see_certificate {
				position: absolute;
				bottom: 0;
				right: 0;
				background-color: #FDD7E0;
				display: flex;
				align-items: center;
				justify-content: center;
				line-height: 1;
				width: 126rpx;
				height: 46rpx;
				border-radius: 16rpx 0px 16rpx 0px;
				color: #FF6084;
				font-size: 22rpx;
			}
		}
	}

	.bottom_btn {
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 8rpx 40rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 8rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 8rpx);
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
		border-top: 1rpx solid #CCCCCC;

		/* view {
			width: 100%;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: linear-gradient(90deg, #FF6188 0%, #FF5252 100%);
			border-radius: 62rpx;
			line-height: 1;
			font-size: 28rpx;
			color: #fff;
		} */

		.bottom_btn_prohibit {}
	}
</style>